//
//  JMBrowserViewController.m
//  VoluntaryStar
//
//  Created by ios on 15/8/17.
//  Copyright (c) 2015年 ios. All rights reserved.
//

#import "JMBrowserViewController.h"
#import "AppDelegate.h"

@interface JMBrowserViewController ()<UIWebViewDelegate,UIScrollViewDelegate>

@property (nonatomic,strong) UIWebView *webView;

@property (nonatomic,strong) UIButton *btn;

@property (nonatomic,strong) UIBarButtonItem *closeButton;

@property (nonatomic,strong) UIBarButtonItem *stopBarItem;

@property (nonatomic,strong) UIBarButtonItem *forwardBarItem;

@property (nonatomic,strong) UIBarButtonItem *nextBarItem;

@property (nonatomic,strong) UIBarButtonItem *shareBarItem;


-(void)forwardWebView;

-(void)backWebView;

-(void)stopWebView;

-(void)loadWebView;

-(void)reloadWebView;

-(void)shareWebView;//扩展
@end

@implementation JMBrowserViewController
#pragma mark  初始化 界面和 webView
-(UIWebView *)webView{
    if(!_webView){
        _webView = [[UIWebView alloc]init];
        _webView.suppressesIncrementalRendering = YES;
        _webView.scalesPageToFit = YES;
        _webView.scrollView.delegate =self;
        self.webView.frame = self.view.bounds;
        self.webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [self.view addSubview:_webView];
    }
    return _webView;
}
-(UIButton *)btn{
    if(!_btn){
        UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
//        [b setTitle:@"返回" forState:UIControlStateNormal];
        [b setImage:[UIImage imageNamed:@"projectImage.bundle/refresh_image_"] forState:UIControlStateNormal];
        [b setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        b.titleLabel.font = [UIFont systemFontOfSize:UI_SCREEN_WITH * 0.046875];
        [b addTarget:self action:@selector(backToparentClass)forControlEvents:UIControlEventTouchUpInside];
        _btn= b;
    }
    return _btn;
}
//-(UIBarButtonItem *)closeButton{
//    if(!_closeButton){
//        self.navigationController.navigationBar.userInteractionEnabled = YES;
//        _closeButton = [[UIBarButtonItem alloc]initWithCustomView:[self backButton]];
//    }
//    return _closeButton;
//}
#pragma mark  返回主界面
-(void)backToparentClass{
    [self backWebView];
//    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark  chu shi hua toolBar shang de an niu
-(UIBarButtonItem *)stopBarItem{
    if(!_stopBarItem){
        _stopBarItem =[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"BrowserToolBarImage.bundle/images/stopButton"] style:UIBarButtonItemStylePlain target:self action:@selector(stopWebView)];
    }
    return _stopBarItem;
}
-(UIBarButtonItem *)forwardBarItem{
    if(!_forwardBarItem){
        _forwardBarItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"BrowserToolBarImage.bundle/images/previousButton"] style:UIBarButtonItemStylePlain target:self action:@selector(backWebView)];
    }
    return _forwardBarItem;
}
-(UIBarButtonItem *)nextBarItem{
    if(!_nextBarItem){
        _nextBarItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"BrowserToolBarImage.bundle/images/nextButton"] style:UIBarButtonItemStylePlain target:self action:@selector(forwardWebView)];
    }
    return _nextBarItem;
}
-(UIBarButtonItem *)shareBarItem{
    if(!_shareBarItem){
        _shareBarItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"BrowserToolBarImage.bundle/images/nextButton"] style:UIBarButtonItemStylePlain target:self action:@selector(shareWebView)];
    }
    return _shareBarItem;
}

#pragma mark   显示界面从这里开始
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    self.backBtn.hidden =YES;
    self.view.backgroundColor = self.backGroundColor;
    self.navigationController.navigationBarHidden = NO;
    self.navigationItem.title =self.titleName;

//    self.navigationItem.leftBarButtonItem = self.closeButton;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.webView];
    [self loadWebView];
    self.btn.frame = CGRectMake(UI_SCREEN_WITH - 60,UI_SCREEN_HEIGHT - 60, 40, 40);
    [self.view addSubview:self.btn];
}

#pragma mark  自定义底部的空间按钮所调用的方法
-(void)forwardWebView{
    if([self.webView canGoForward]){
        [self.webView goForward];
    }
}
-(void)backWebView{
    if([self.webView canGoBack]){
        [self.webView goBack];
    }
}
-(void)stopWebView{
    [self.webView stopLoading];
}
-(void)loadWebView{
    NSURLRequest *request = [NSURLRequest requestWithURL:self.url];
    [self.webView loadRequest:request];
}
-(void)reloadWebView{
    [self.webView reload];
}
-(void)shareWebView{

}//扩展

#pragma mark - UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webview shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
//Little timer to avoid loading lags
//    NSTimer *webTimer = [NSTimer timerWithTimeInterval:kWebLoadingTimout target:self
//                                              selector:@selector(reachabilityChanged)
//                                              userInfo:nil
//                                               repeats:NO];
//    [[NSRunLoop mainRunLoop] addTimer:webTimer forMode:NSDefaultRunLoopMode];
//    self.currentURL = request.URL;
//    _stopButton.enabled = YES;
    return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webview
{

}
- (void)webViewDidFinishLoad:(UIWebView *)webview
{
    
}
- (void)webView:(UIWebView *)webview didFailLoadWithError:(NSError *)error
{
    [self webViewDidFinishLoad:webview];
}
#pragma mark  scrollView delegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
   
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
